import { alertMessage, isValidateNull } from '@/utils/utils';
import { changePasswordApi } from './change-login-password.service';
export default {
  name: 'change-login-password',
  components: {},
  props: {
    userName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showDialog: true,
      title: '修改密码',
      editForm: {},
      editRules: {
        oldPassword: [{ required: true, trigger: "change", validator: isValidateNull }],
        newPassword: [{ required: true, trigger: "change", validator: isValidateNull }]
      } // 编辑表单验证规则
    }
  },
  mounted() {
  },
  methods: {
    // 修改密码
    save() {
      if (this.editForm.newPassword !== this.editForm.renewPassword) {
        alertMessage('两次密码输入不一致,请重新输入', 'warning');
        return
      }
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.editForm.userName = this.userName;
          changePasswordApi(this.editForm).then((res) => {
            this.loading = false;
            alertMessage(res.message);
            this.closeDialog();
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        } else {
          this.loading = false
        }
      });
    },
    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.showDialog = false;
      this.$emit('close', true);
    }
  }
}
